package com.client;

import java.nio.charset.Charset;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import com.gateway.core.MyBuffer;
import com.gateway.core.MyProtocol;

public class ClientProtocolEncoder extends ProtocolEncoderAdapter {

  @SuppressWarnings("unused")
  private Charset charset;

  public ClientProtocolEncoder(Charset charset) {
    this.charset=charset;
  }

  /**
   * 对网关编码
   */
  public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) throws Exception {
    MyProtocol myProtocol=(MyProtocol)obj;
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.put(myProtocol.getType());// type
    buffer.putInt(myProtocol.getBodyLength());// length
    buffer.put(myProtocol.getBodyData());// body
    buffer.flip();
    out.write(buffer.getIoBuffer());
  }
}
